<template>
  <div class="home">

    <div class="headerbox">
      <div class="box2" v-for="item in headers" :style="{ backgroundColor: item[0].color }">
        <section v-for="data in item">
          <span class="number">{{ data.top }}</span>
          <span>{{ data.title }}</span>
        </section>
      </div>
    </div>

    <div class="echartsbox">
      <div class="echarts" ref="echartsRef"></div>
      <div class="colorboxs">
        <div class="colorbox" v-for="item in echartslist">
          <section class="colorimg" :style="{ backgroundColor: item.color }"></section>
          <span>{{ item.name }}</span>
        </div>
      </div>
      <button class="export" @click="exportFile">导出明细</button>
    </div>
  </div>
</template>

<script  lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineComponent } from 'vue';
import {gethomedata} from '@/api/home'
// echarts 模块导入
import * as echarts from 'echarts/core';
import { GridComponent } from 'echarts/components';
import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
//xlsx 模块导入
import { utils, writeFileXLSX } from 'xlsx'



export default defineComponent({
  name: 'home',
  globals: {
   defineEmits: "readonly",
   defineProps: "readonly",
   defineExpose: "readonly",
   withDefaults: "readonly",
   WeixinJSBridge: "readonly",
   ElMessage: "readonly",
   ElMessageBox: "readonly",
   ElLoading: "readonly",
 },
  data() {
    return {
      echartslist: [
        {
          name: '配送量',
          data: [820, 2932, 3901, 4934],
          type: 'line',
          smooth: true,
          color: 'aqua',
          symbol: 'none'
        },
        {
          name: '预收款',
          data: [1820, 2332, 3301, 4734],
          type: 'line',
          smooth: true,
          color: '#000',
          symbol: 'none'
        },
        {
          name: '实收款',
          data: [1300, 1932, 2901, 4334],
          type: 'line',
          smooth: true,
          color: 'pink',
          symbol: 'none'
        },
        {
          name: '毛利',
          data: [3300, 2932, 1901, 4534],
          type: 'line',
          smooth: true,
          color: 'red',
          symbol: 'none'
        },
      ],
      headers: [
        [
          {
            top: 25400,
            title: '一周流水收入',
            color: 'rgb(254, 87, 34)',
          },
          {
            top: 25400,
            title: '一周营业收入',
          }
        ],
        [
          {
            top: 50800,
            title: '一周毛利',
            color: 'orange',
          },
          {
            top: "32%",
            title: '毛利率',
          }
        ],
        [
          {
            top: 264,
            title: '一周配送量',
            color: 'green',
          }
        ],
        [
          {
            top: '60%',
            title: '连续购餐率',
            color: 'rgb(25, 158, 216)',
          }
        ],
        [
          {
            top: 50800,
            title: '一周流水支出',
            color: 'rgb(75, 158, 216)',
          }
        ],
      ],
    }
  },
  async created(){
    const res = await gethomedata()
    // @ts-ignore
    const  {list ,headlist} = res
    this.echartslist = list
    this.headers = headlist
    this.echartsload(this.echartslist)
  },
  mounted() {
    
  },
  methods: {
    // 折线图数据加载
    echartsload(list:any[]){
      echarts.use([GridComponent, LineChart, CanvasRenderer, UniversalTransition]);
    // @ts-ignore
    const myChart = echarts.init(this.$refs.echartsRef);
    const option = {
      xAxis: {
        type: 'category',
        data: ['第一周', '第二周', '第三周', '第四周']
      },
      yAxis: {
        type: 'value',
        splitNumber: 10,
      },
      series: list
    };
    option && myChart.setOption(option);
    },
    // 导出
    exportFile() {
      // 确认组件
      ElMessageBox.confirm(
        '是否确认导出Exel表格',
        '下载确认',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'success',
        }
      )
        .then(() => {
          // 创建导出数据
          // @ts-ignore
          const list = this.echartslist.map(item => {
            return [item.name, ...item.data]
          })
          list.unshift(['', '第一周', '第二周', '第三周', '第四周',])
          // 简历工作表  数组转工作表格式
          const ws = utils.json_to_sheet(list)
          // 创建工作簿
          const wb = utils.book_new()
          // 工作表插入工作簿
          utils.book_append_sheet(wb, ws, '近期营业状况')
          writeFileXLSX(wb, `近期营业状况-${new Date().toISOString()}.xlsx`)
          ElMessage({
            type: 'success',
            message: '导出成功',
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消导出',
          })
        })
    },
  },

})
</script>

<style scoped lang="less">
// 原生HTML 样式

// 头部
.headerbox {
  display: flex;
  max-width: 1320px;

  .box2 {
    width: 277px;
    height: 146px;
    display: flex;
    justify-content: space-around;
    color: #fff;
    margin: 0 20px;

    section {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-around;

      .number {
        font-size: 32px
      }

      ;
    }

    ;
  }
}

// 折线图
.echartsbox {
  padding: 0 0 0 50px;
  margin-top: 20px;
  box-sizing: border-box;
  width: 1300px;
  height: 800px;
  box-shadow: 1px 1px 40px 2px #ccc;
  position: relative;

  .echarts {
    width: 1200px;
    height: 700px;
  } ;

  .colorboxs {
    display: flex;
    padding-left: 100px;

    .colorbox {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin: 0 50px;

      .colorimg {
        width: 100px;
        height: 30px;
        background-color: aqua;
        margin-bottom: 10px;
      }
    }
  }

  .export {
    position: absolute;
    right: 20px;
    top: 20px;
    width: 154px;
    height: 65px;
    background-color: #ccc;
    border: none;
    cursor: pointer;
    font-size: 20px;
    line-height: 60px;
  }
}
</style>
